﻿# 配置镜像名称和版本
$customImageName = "abp_template" # 配置镜像名称，不包括 registry.cn-chengdu.aliyuncs.com/jycd/ 这部分
$customImageVersion = "master" # 配置镜像版本

# 设置版本
$version = if ($customImageVersion) { $customImageVersion } else { Read-Host "Please input version (default:latest)" }
if ([string]::IsNullOrEmpty($version)) {
    $version = "latest"
}

# 获取项目文件
$projectFile = Get-ChildItem -Path $PSScriptRoot -Filter *.csproj | Select-Object -First 1
if (-not $projectFile) {
    Write-Error "未找到 .csproj 文件，请确保脚本位于项目根目录下。"
    Read-Host -Prompt "按 Enter 键继续..."
    exit 1
}

# 尝试从 .csproj 文件中提取 AssemblyName
[xml]$projectXml = Get-Content $projectFile.FullName
$assemblyNameNode = $projectXml.Project.PropertyGroup.AssemblyName

if ($assemblyNameNode) {
    $projectName = $assemblyNameNode
} else {
    # 如果无法提取 AssemblyName，则使用项目文件名（去掉扩展名）
    $projectName = [System.IO.Path]::GetFileNameWithoutExtension($projectFile.Name)
}

Write-Host "Detected Project Name: $projectName"

# 读取 .NET 版本
$net_version = Select-Xml -Path $projectFile.FullName -XPath '//TargetFramework' | Select-Object -ExpandProperty Node | ForEach-Object { $_.InnerText }
if (-not $net_version) {
    $net_version = Select-Xml -Path $projectFile.FullName -XPath '//TargetFrameworks' | Select-Object -ExpandProperty Node | ForEach-Object { $_.InnerText.Split(';')[0] }
}
$net_version = $net_version -replace 'net', ''
$net_version = ($net_version -split '\.')[0,1] -join '.'
Write-Host "Detected .NET version: $net_version"

# 定义输出目录
$OUTPUT_DIR = "$PSScriptRoot\bin\publish"

# 清理先前的发布输出
if (Test-Path $OUTPUT_DIR) {
    Remove-Item -Recurse -Force $OUTPUT_DIR
}

# 发布项目
Write-Host "Publishing project..."
dotnet publish $PSScriptRoot -c Release -o $OUTPUT_DIR
if ($LASTEXITCODE -ne 0) {
    Write-Host "Error during project publish."
    exit $LASTEXITCODE
} else {
    Write-Host "Project published successfully."
}

# 获取项目主DLL名称
$dll_name = "$projectName.dll"
Write-Host "Detected DLL name: $dll_name"

# 构造镜像名称
$image_name_parts = if ($customImageName) { $customImageName } else { $projectName.ToLower() -split '\.' }
$DOCKER_IMAGE_NAME = "registry.cn-chengdu.aliyuncs.com/jycd/$($image_name_parts -join '_'):$version"

# 生成 Dockerfile 到发布目录
$DOCKERFILE_PATH = "$OUTPUT_DIR\Dockerfile"
Write-Host "Generating Dockerfile..."
@"
FROM registry.cn-chengdu.aliyuncs.com/jycdgg/dotnet-aspnet:$net_version AS base
WORKDIR /app
EXPOSE 8080

FROM base AS final
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "$dll_name"]
"@ | Out-File -FilePath $DOCKERFILE_PATH -Encoding utf8

# 替换 Dockerfile 中的变量
(Get-Content $DOCKERFILE_PATH) -replace '\$net_version', $net_version -replace '\$dll_name', $dll_name | Set-Content $DOCKERFILE_PATH

# 构建镜像
Write-Host "Building Docker image..."
docker build -f $DOCKERFILE_PATH -t $DOCKER_IMAGE_NAME $OUTPUT_DIR
if ($LASTEXITCODE -ne 0) {
    Write-Host "Error during Docker image build."
    exit $LASTEXITCODE
} else {
    Write-Host "Docker image built successfully."
}

# 推送到镜像仓库
Write-Host "Pushing Docker image to registry..."
docker push $DOCKER_IMAGE_NAME
if ($LASTEXITCODE -ne 0) {
    Write-Host "Error during Docker image push."
    exit $LASTEXITCODE
} else {
    Write-Host "Docker image pushed successfully."
}

# 完成
Write-Host "All steps completed successfully. Press any key to continue..."
Read-Host



